package cn.eric.game.fujiatianxia6.po;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

// 城市建筑物工厂类
public class BuildingFactory {
	
	public static List<Building> buildings = new ArrayList<>();
	
	public static void initBuildings(){
		buildings.add(new Building(1, "城墙", "防守时增加进攻方损失10%", 10, 1,5000,5000));
		buildings.add(new Building(2, "弩炮", "防守时增加进攻方损失10%", 10, 1,5000,5000));
		buildings.add(new Building(3, "市场", "增加繁荣度50%", 50, 2,10000,10000));
		buildings.add(new Building(4, "道路", "增加繁荣度50%", 50, 2,10000,10000));
		buildings.add(new Building(7, "马厩", "每回合结束驯养马匹，将城市内的剑兵转换成骑兵", 50, 3,20000,20000));
		buildings.add(new Building(8, "兵器厂", "每回合结束生产枪和弓，并将城市内的剑兵转换成弓兵和枪兵", 25, 3,20000,20000));
		buildings.add(new Building(9, "徽兵所", "每回合结束招募剑兵", 25, 3,20000,20000));
	}
	
	/**
	 * 
	* @Title: showBuildings
	* @Description: 显示可选建筑的名称
	* @param     设定文件
	* @return void    返回类型
	* @throws
	 */
	public static void showBuildings(){
		System.out.println("建筑为");
		for (Building building : buildings) {
			System.out.println(building.toString());
		}
	}
	
	/**
	 * 
	* @Title: showBuildings
	* @Description: 展示目前城市已有的建筑名称
	* @param @param city    设定文件
	* @return void    返回类型
	* @throws
	 */
	public static void showBuildings(City city){
		System.out.println("城市内已有建筑为");
		if( city!=null && city.getBildings().size()>0){
			for (Building building : city.getBildings()) {
				System.out.println(building.toString());
			}
		}
	}
	
	public static void buildInCity(City city,General general){
		System.out.println("是否要建设新的建筑");
		// 首先看还有没有空地
		if(city.getBlank() <= 0){
			System.out.println("没有空地了，无法建设");
		}else{
			while(true){
				System.out.println("请选择您想要建设的建筑,0表示不建设");
				showBuildings();
				Scanner input = new Scanner(System.in);
				int choise = input.nextInt();
				if(choise==0){
					break;
				}else if(choise<0 || choise>12){
					System.out.println("数字错误，请重新选择");
				}else if( (choise == 1 && (countBuildsInCity(city,1) < 4)) || (choise == 2 && (countBuildsInCity(city,2) < 4))
						|| (choise == 3 && (countBuildsInCity(city,1) < 3)) || (choise == 4 && (countBuildsInCity(city,4) < 1))
						|| (choise == 7 && (countBuildsInCity(city,1) < 7)) || (choise == 8 && (countBuildsInCity(city,1) < 8))){

					if(general.getMoney() < getBuildById(choise).purchase){
						System.out.println("对不起您的钱不够");
					}else{
						try{
							city.getBildings().add(getBuildById(choise));
							general.setMoney(general.getMoney() - getBuildById(choise).purchase);
							resetCityByNewBilding(city,choise);
						}catch(Exception e){
							e.printStackTrace();
							System.out.println("出错啦");
						}
						break;
					}
				}else{
					System.out.println("不能重复建设，请重新选择");
				}
			}
			
		}
	}
	
	// 根据新建的
	private static void resetCityByNewBilding(City city, int choise) {
		if(choise == 3 || choise == 4){
			city.setProsperity((int)(city.getProsperity() * 1.5));
		}		
	}

	// 根据ID返回建筑
	public static Building getBuildById(int choise) {
		if(choise>0){
			for (Building building : buildings) {
				if(building.id == choise){
					return building;
				}
			}
		}
		return null;
	}

	// 计算城市内该建筑的数量
	public static int countBuildsInCity(City city, int id) {
		int count = 0;
		if( city!=null && city.getBildings() != null && city.getBildings().size()>0){
			for (Building building : city.getBildings()) {
				if(building.id == id){
					count++;
				}
			}
		}
		return count;
	}
}
